package com.ruoyi.project.buildexperiment.service;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.buildexperiment.domain.AddExperimentParam;
import com.ruoyi.project.buildexperiment.domain.ExperimentDto;
import com.ruoyi.project.buildexperiment.domain.ExperimentParam;
import com.ruoyi.project.buildexperiment.domain.LabDto;
import com.ruoyi.project.buildexperiment.mapper.ExperimentMapper;
import com.ruoyi.project.myexperiment.domain.DetailsDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

import static com.ruoyi.framework.web.domain.AjaxResult.error;

@Service
public class BuildExperimentServiceImpl implements BuildExperimentService{
    private static final Logger log = LoggerFactory.getLogger(BuildExperimentServiceImpl.class);
    @Autowired
    private ExperimentMapper experimentMapper;


    @Override
    public List<ExperimentDto> selectBuildexperiment(ExperimentParam experimentParam) {
        System.err.println(123);
        return experimentMapper.selectBuildexperiment(experimentParam);
    }

    @Override
    public List<LabDto> selectLab(Long userId) {
        return experimentMapper.selectLab(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertExperiment(AddExperimentParam experimentParam) {
        String upload=null;

        if (experimentMapper.insertExperiment(experimentParam) > 0)
        {
            return new AjaxResult(AjaxResult.Type.SUCCESS,"添加成功！");
        }
       /* try
        {
            if (experimentParam.getFileId()!=null)
            {
                upload= FileUploadUtils.upload(RuoYiConfig.getUploadPath(), experimentParam.getFileId());
                experimentParam.setUploadPath(upload);
                if (experimentMapper.insertExperiment(experimentParam) > 0)
                {
                    return new AjaxResult(AjaxResult.Type.SUCCESS,"添加成功！");
                }
            }
            return error();
        }
        catch (Exception e)
        {
            String path=RuoYiConfig.getUploadPath()+upload.substring(upload.indexOf("upload")+6,upload.length());
            File file2=new File(path);
            if(file2.exists()){
                Boolean result=file2.delete();
            }
            log.error("新增实验错误！", e);
            return new AjaxResult(AjaxResult.Type.ERROR,"添加失败！");
//            return error(e.getMessage());
        }*/
        return new AjaxResult(AjaxResult.Type.ERROR,"添加失败！");
    }

    @Override
    public int issue(Long id) {
        return experimentMapper.issue(id);
    }

    @Override
    @Transactional
    public int deleteById(Long id) {
        //在删除实验前，将学生提交实验信息的表student_experiment中的有关信息也同样删除
        experimentMapper.deleteStuExById(id);
        return experimentMapper.deleteById(id);
    }




    @Override
    public ExperimentDto getExperimentDtoById(Long id) {
        return experimentMapper.getExperimentDtoById(id);
    }

    @Override
    @Transactional
    public boolean updateExperiment(ExperimentDto experimentDto) {
        //修改实验
        /**
         * 1.修改实验前，将修改的信息再学生提交实验表中同步修改
         *即该实验已有学生提交修改信息，提交的实验的信息也修改
         */
        DetailsDto detailsDto=new DetailsDto();
        long id=experimentDto.getId();
        detailsDto.setId((int)id);
        detailsDto.setIntroduce(experimentDto.getExperimentIntroduce());
        detailsDto.setExperimentName(experimentDto.getExperimentName());
        detailsDto.setExperimentTypes(experimentMapper.selectType(experimentDto));
        detailsDto.setCourseName(experimentMapper.selectCourseName(experimentDto));
        detailsDto.setExperimentMessage(experimentMapper.selectExperimentMessage(experimentDto));
        experimentMapper.updateStudentExperiment(detailsDto);
        /**
         * 之后修改实验信息
         */
        return experimentMapper.updateExperiment(experimentDto);
    }


    /**
     * 批量删除
     */
    @Override
    public int deletes(String ids){
        experimentMapper.deletesStuExps(Convert.toStrArray(ids));
        return experimentMapper.deletes(Convert.toStrArray(ids));
    }


    /**
     * 判断预约的实验室是否已经布置了实验
     * @param experimentLabId
     * @return
     */
    @Override
    public Long ifExits(String experimentLabId) {
        return experimentMapper.ifExits(experimentLabId);
    }


}
